package com.aiolos.springboot.stepbystep.error;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局统一业务异常处理类
 */
@ControllerAdvice(basePackages ={"com.aiolos.springboot.stepbystep",})
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler({BusinessException.class})
    @ResponseBody
    public ErrorInfo defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception{
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setErrorMessage(e.getMessage());
        errorInfo.setErrorUrl(req.getRequestURI());
        errorInfo.setErrorCode(ErrorInfo.ERROR);
        return errorInfo;
    }
}
